package com.ruoyi.app.mapper;

import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.app.domain.RestockInfo;
import com.ruoyi.app.domain.vo.RestockInfoVo;
import com.ruoyi.app.domain.vo.RestockProductVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 进货单Mapper接口
 * 
 * @author ruoyi
 * @date 2024-12-12
 */
public interface RestockInfoMapper extends BaseMapper<RestockInfo> {

    /**
     * 统计
     * @param restockInfoVo
     * @return
     */
    List<RestockInfoVo> statistics(@Param("restockInfoVo") RestockInfoVo restockInfoVo);

    /**
     * 列表统计
     * @param restockInfoVo
     * @return
     */
    @Select("SET @row_index := 0; " +
            "select CATEGORY_TXT,SUM(TOTAL_PURCHASING_PRICE) TOTAL_PURCHASING_PRICE, SUM(TOTAL_NUMBER) TOTAL_NUMBER from ( " +
            "select IF(ind > 3, '其他', CATEGORY_TXT) CATEGORY_TXT, TOTAL_PURCHASING_PRICE, TOTAL_NUMBER from ( " +
            "select p.CATEGORY_TXT,SUM(p.TOTAL_PURCHASING_PRICE) TOTAL_PURCHASING_PRICE,SUM(p.TOTAL_NUMBER) TOTAL_NUMBER,(@row_index := @row_index + 1) ind " +
            "from restock_info i INNER JOIN restock_product p ON i.ID = p.RESTOCK_ID AND p.DEL_FLAG = 0 " +
            "WHERE i.DEL_FLAG = 0 AND i.SHOP_ID = #{restockInfoVo.shopId} AND i.CREATE_TIME BETWEEN " +
            "#{restockInfoVo.startDate} AND #{restockInfoVo.endDate} GROUP BY p.CATEGORY_TXT ORDER BY TOTAL_PURCHASING_PRICE DESC " +
            ") a) b GROUP BY CATEGORY_TXT ORDER BY TOTAL_PURCHASING_PRICE DESC")
    List<RestockProductVo> statisticsCategoryList(@Param("restockInfoVo") RestockInfoVo restockInfoVo);

    /**
     * 总和统计
     * @param restockInfoVo
     * @return
     */
    @Select("select SUM(TOTAL_NUMBER) TOTAL_NUMBER, SUM(TOTAL_PRICE) TOTAL_PRICE from " +
            "restock_info WHERE DEL_FLAG = 0 AND SHOP_ID = #{restockInfoVo.shopId} AND CREATE_TIME BETWEEN " +
            "#{restockInfoVo.startDate} AND #{restockInfoVo.endDate} GROUP BY SHOP_ID ")
    RestockInfoVo statisticsInfo(@Param("restockInfoVo") RestockInfoVo restockInfoVo);

    /**
     * 本月销售金额和数量
     * @param shopId
     * @return
     */
    @Select("select SUM(TOTAL_NUMBER) TOTAL_NUMBER, SUM(TOTAL_PRICE) TOTAL_PRICE from restock_info WHERE DEL_FLAG = 0 " +
            "AND SHOP_ID = #{shopId}  AND DATE_FORMAT(CREATE_TIME, '%Y-%m') = DATE_FORMAT(CURRENT_DATE(), '%Y-%m') GROUP BY SHOP_ID")
    RestockInfoVo currentMonthStatistics(@Param("shopId") String shopId);
}
